<?php

/**
 * This is the model class for table "{{comment}}".
 *
 * The followings are the available columns in table '{{comment}}':
 * @property integer $id
 * @property integer $article_id
 * @property string $comment_text
 * @property string $created_by
 * @property string $create_date
 * @property integer $status
 * @property string $update_date
 * @property string $updated_by
 */
class Comment extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Comment the static model class
     */
    public static function model($className = __CLASS__) {
	return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
	return '{{comment}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
	// NOTE: you should only define rules for those attributes that
	// will receive user inputs.
	return array(
	    array('article_id, comment_text', 'required'),
	    array('article_id, status', 'numerical', 'integerOnly' => true),
	    array('comment_text', 'length', 'max' => 255),
	    array('created_by, updated_by', 'length', 'max' => 50),
	    array('create_date, update_date', 'safe'),
	    // The following rule is used by search().
	    // Please remove those attributes that should not be searched.
	    array('id, article_id, comment_text, created_by, create_date, status, update_date, updated_by', 'safe', 'on' => 'search'),
	);
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
	// NOTE: you may need to adjust the relation name and the related
	// class name for the relations automatically generated below.
	return array(
	    'commentRelation' => array(self::BELONGS_TO, 'Article', 'article_id'),
	);
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
	return array(
	    'id' => 'ID',
	    'article_id' => 'Article',
	    'comment_text' => 'Comment Text',
	    'created_by' => 'Created By',
	    'create_date' => 'Create Date',
	    'status' => 'Status',
	    'update_date' => 'Update Date',
	    'updated_by' => 'Updated By',
	);
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
	// Warning: Please modify the following code to remove attributes that
	// should not be searched.

	$criteria = new CDbCriteria;

	$criteria->compare('id', $this->id);
	$criteria->compare('article_id', $this->article_id);
	$criteria->compare('comment_text', $this->comment_text, true);
	$criteria->compare('created_by', $this->created_by, true);
	$criteria->compare('create_date', $this->create_date, true);
	$criteria->compare('status', $this->status);
	$criteria->compare('update_date', $this->update_date, true);
	$criteria->compare('updated_by', $this->updated_by, true);

	return new CActiveDataProvider($this, array(
	    'criteria' => $criteria,
	));
    }
    
      /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function searchComment() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.
        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('article_id', $this->article_id);
        $criteria->compare('comment_text', $this->comment_text, true);
        $criteria->compare('created_by', $this->created_by, true);
        $criteria->compare('create_date', $this->create_date, true);
        $criteria->compare('status', $this->status, true);
        //$criteria->condition = '(article_id LIKE :keyword_article_id)'; // AND comment_text LIKE :keyword_comment_text AND status LIKE :keyword_status
        //$criteria->params = array(':keyword_article_id' => '%' . $this->id . '%'); //,':keyword_comment_text' => '%' . $this->comment_text . '%',':keyword_status' => '%' . $this->status . '%'     $criteria->params = array(':keyword_article_id' => '%' . $this->article_id . '%'); //,':keyword_comment_text' => '%' . $this->comment_text . '%',':keyword_status' => '%' . $this->status . '%'
        return new CActiveDataProvider($this, array(
                    'criteria' => $criteria,
                    'sort' => array(
                        'defaultOrder' => array(
                            'create_date' => CSort::SORT_ASC,
                        )
                    ),
                ));
    }

    protected function afterFind() {
	// convert to display format
	$this->update_date = strtotime($this->update_date);
	$this->update_date = date(LGConstant::DATE_FORMAT, $this->update_date);
	if ($this->update_date == LGConstant::DATE_DEFAULT)
	    $this->update_date = '';

	$this->create_date = strtotime($this->create_date);
	$this->create_date = date(LGConstant::DATE_FORMAT, $this->create_date);
	if ($this->create_date == LGConstant::DATE_DEFAULT)
	    $this->create_date = '';

	parent::afterFind();
    }

    public static function GetCommentStatus() {
	return Html::listData(LGHelpers::getCommentStatus(), 'id', 'name');
    }

    public function GetListByArticleId($article_id) {
	$criteria = new CDbCriteria();
	$criteria->addCondition("article_id = :article_id and status = :status");
	$criteria->params = array(':article_id' => $article_id, 'status' => 1);
	$criteria->order = 'create_date DESC';
	return $this->findAll($criteria);
    }
    
    public function beforeSave() {
        if (parent::beforeSave()) {
            $current_date = date(LGConstant::DATE_FORMAT_FOR_DATABASE);
            $logedInUser = User::model()->findByPk(Yii::app()->user->id);

            if ($this->create_date == LGConstant::DATE_DEFAULT) {
                $this->create_date = $current_date;
            }

            if ($this->created_by == NULL) {
                $this->created_by = $logedInUser->username;
            }

            $this->update_date = $current_date;
            $this->updated_by = $logedInUser->username;

            return true;
        }
        else
            return false;
    }

}